package review.Day_0401;

import org.springframework.util.StringUtils;

import java.util.Stack;

/**
 * @author zxc
 * @date 2023/04/01 17:38
 **/
public class Solution02 {
    // 思路 ： 即，使用辅助栈,用来存储数字
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            if(token.equals("+")){
                Integer value1 = stack.pop();
                Integer value2 = stack.pop();
                stack.push(value1 + value2);
            }
            else if(token.equals("-")){
                Integer value1 = stack.pop();
                Integer value2 = stack.pop();
                stack.push(value2 - value1);
            }
            else if(token.equals("*")){
                Integer value1 = stack.pop();
                Integer value2 = stack.pop();
                stack.push(value1 * value2);
            }
            else if(token.equals("/")){
                Integer value1 = stack.pop();
                Integer value2 = stack.pop();
                stack.push(value2 / value1);
            }
            else {
                stack.push(Integer.valueOf(token));
            }
        }
        return stack.pop();
    }
}
